/**
 * 跳转有两个主要限制:只能在单个函数的范围内跳转(不能从一个函数跳转到另一个函数)，如果跳转，不能跳过任何变量的初始化过程。
 * 
 * 最佳实践
 *   不使用 goto 语句
 * 
 */

#include <iostream>
#include <cmath> // for sqrt() function


void printCats(bool skip)
{
    if (skip)
        goto end; // jump forward; statement label 'end' is visible here due to it having function scope

    std::cout << "cats\n";
end:
    ; // statement labels must be associated with a statement
}


void example() {
    // goto skip;   // error: this jump is illegal because...
    int x { 5 }; // this initialized variable is still in scope at statement label 'skip'
skip:
    x += 3;      // what would this even evaluate to if x wasn't initialized?
}


int main()
{
    double x{};
tryAgain: // this is a statement label
    std::cout << "Enter a non-negative number: ";
    std::cin >> x;

    if (x < 0.0)
        goto tryAgain; // this is the goto statement

    std::cout << "The square root of " << x << " is " << std::sqrt(x) << '\n';



    printCats(true);
    printCats(false);

    return 0;
}